<template>
	<view v-if="popup && popup.type">
		<uni-popup ref="popup_dialog" type="center" :maskClick="(popup && popup.must_click)?false:true">
			<view class='box' style="width: 90%;margin-left:5%" v-if="popup && popup.type==='dialog'">
				<view class='boxHead orderTitle'>
					<text class="title">{{popup.title}}</text>
					<uni-icons type="closeempty" size="20" color='#ccc' @click="close()" v-if="!popup.must_click"></uni-icons>
				</view>
				<view class='boxBody'>
					<view class="margin-top-15">
						<text style="font-size: 20px;font-weight: 400;">{{popup.content}}</text>
					</view>
				</view>
				<view class="textBlock line-height-25 margin-top-20" v-if="popup.tips">
					<view class="tips">{{popup.tips}}</view>
				</view>						
				<view class="flexBtn margin-top-20">
					<button v-if="popup.cancel_text" style='color:white;background:#FF9700' @click="goPopupPage(popup.cancel_page)">{{popup.cancel_text}}</button>
					<button v-if="popup.ok_text" style='color:white;background:#00a4ff' @click="goPopupPage(popup.ok_page)">{{popup.ok_text}}</button>
				</view>	
			</view>
			<view class='center' style="flex-direction: column;width: 76%;margin-left:12%;"  v-if="popup && popup.type==='image'">
				<image @click="goPopupPage(popup.image_page)" :src="popup.image_url" style="width:100%;border-radius: 10px;" mode="widthFix"></image>
				<uni-icons type="close" size="33" color='#ccc' @click="close()"></uni-icons>
			</view>
			<view class="box"v-if="popup && popup.type==='items'">
				<view class='boxHead orderTitle'>
					<text class="title">{{popup.title||'请先完成以下流程'}}</text>
					<uni-icons type="closeempty" size="20" color='#ccc' @click="close()" v-if="!popup.must_click"></uni-icons>
				</view>
				<view class="boxBody" style="width:100%;">
					<view class="uni-list">
						<label class="radio_item" v-for="(item,index) in popup.items" v-if="!item.pass">
							<uni-icons type="close" color="red"></uni-icons>
							<text>{{item.title}}</text>
						</label>
					</view>
				</view>
				<view class="flexBtn margin-top-20">
					<button v-if="popup.cancel_page" @click="goPopupPage(popup.cancel_page)">{{popup.cancel_text}}</button>
					<button v-else-if="!popup.must_click" @click="close()">{{popup.cancel_text||'再看看'}}</button>
					<button @click="goPopupPage(popup.ok_page)">{{popup.ok_text}}</button>
				</view>				
			</view>
			<view class="box"v-if="popup && popup.type==='input'">
				<view class='boxHead orderTitle'>
					<text class="title">{{popup.title||'请先完成以下流程'}}</text>
					<uni-icons type="closeempty" size="20" color='#ccc' @click="close()" v-if="!popup.must_click"></uni-icons>
				</view>
				<view class="boxBody">
					<view class="row-box row-form" v-if="popup.items && popup.items.length>0">
						<view class="row-border" v-for="(item,index) in popup.items" :key="index">
							<view class='row-cell' v-if="item.attr_type=='text'">
								<p-check-text @change="checkRuleChange" v-model="popup.items[index].value" :field="item.attr_name" :textClass="item.required?'required':'not-required'" :tip="item.placeholder || item.attr_name+'未填写'">{{item.attr_name}}</p-check-text>
								<view class="right">
									<input v-model="popup.items[index].value" @change="onChange" :placeholder="item.placeholder"  placeholder-class="tips"/>
									<text class="margin-left" v-if="item.attr_unit">{{item.attr_unit}}</text>
								</view>	
							</view>
							<view class='row-cell' v-if="item.attr_type=='label'">
								<p-check-text @change="checkRuleChange" v-model="popup.items[index].value" :field="item.attr_name" :textClass="item.required?'required':'not-required'" :tip="item.placeholder || item.attr_name+'未填写'">{{item.attr_name}}</p-check-text>
								<view>
									<text class="margin-left" v-if="item.attr_unit">{{item.placeholder}}</text>
								</view>	
							</view>
							<view class='row-cell' v-if="item.attr_type=='number'">
								<p-check-text @change="checkRuleChange" v-model="popup.items[index].value" :field="item.attr_name" :textClass="item.required?'required':'not-required'" :tip="item.placeholder || item.attr_name+'未填写'">{{item.attr_name}}</p-check-text>
								<view class="right">
									<uni-number-box v-model="popup.items[index].value" :min="0" @change="onChange"/>
									<text class="margin-left" v-if="item.attr_unit">{{item.attr_unit}}</text>
								</view>	
							</view>
							<view class='row-cell row-col' v-else-if="item.attr_type=='textarea'">
								<p-check-text @change="checkRuleChange" v-model="popup.items[index].value" :field="item.attr_name" :textClass="item.required?'required':'not-required'" :tip="item.placeholder || item.attr_name+'未填写'">{{item.attr_name}}</p-check-text>
								<view class="padding-bottom-10">
									<p-text-area v-model="popup.items[index].value" :placeholder="item.placeholder" placeholder-class="tips" @change="onChange"></p-text-area>
									
								</view>				
							</view>
							<view class='row-cell row-col' v-else-if="item.attr_type=='image'">
								<p-check-text @change="checkRuleChange" v-model="popup.items[index].value" :field="item.attr_name" :textClass="item.required?'required':'not-required'" :tip="item.placeholder || item.attr_name+'未上传'">{{item.attr_name}}</p-check-text>
								<view class="padding-left-10">
									<p-input-upload v-model="popup.items[index].value" @change="onChange" :upload_count="1" mode="image"></p-input-upload>
								</view>	
							</view>
							<view class='row-cell' v-else-if="'radio,select,checkbox,radio_button,check_buttton'.indexOf(item.attr_type)>=0">
								<p-check-text @change="checkRuleChange" v-model="popup.items[index].value" :field="item.attr_name" :textClass="item.required?'required':'not-required'" :tip="item.placeholder || '请选择'+item.attr_name">{{item.attr_name}}</p-check-text>
								<view class="right" @click="openPopup('popup_'+item.id)" >
									<text v-if="item.value">{{item.value}}</text>
									<text v-else class="tip">请选择{{item.attr_name}}</text>
									<uni-icons type="arrowright" size="20" color='#ccc'></uni-icons>
								</view>	
							</view>
						</view>
						
					</view>
				</view>
				<view class="flexBtn margin-top-20">
					<button v-if="popup.cancel_page" @click="goPopupPage(popup.cancel_page)">{{popup.cancel_text}}</button>
					<button v-else-if="!popup.must_click" @click="close()">{{popup.cancel_text||'再看看'}}</button>
					<button @click="submit()">{{popup.ok_text}}</button>
				</view>	
				<view v-for="(item,index) in popup.items" :key="index">
					<p-popup-sheet v-if="'radio,select,checkbox,radio_button,check_buttton'.indexOf(item.attr_type)>=0" :ref="'popup_'+item.id" v-model="popup.items[index].value" :list="item.values" :column="1" :type="item.attr_type" :okTitle="item.attr_type.indexOf('check')>=0?'确认':''" returnType="name" @change="onChange"></p-popup-sheet>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import request from '../../libs/request.js';
	export default {
		props: {
			value: {
				type: [Array,Object],
				default () {
					return {}
				}
			},
		},
		data() {
			return {
				popup:{
					
				},
				visible:false,
			}
		},
		created() {
			
		},
		mounted() {
			
	
		},
		watch:{
			value: {
				handler(newVal) {
					//console.log('watch',newVal);
					if (newVal){
						this.popup = Object.assign({},newVal);
						this.$nextTick(function () {
							this.check();
						})
					}
				},
				immediate: true
			},
		},

		methods: {
			onChange(){
				//console.log('goods.change1',this.goods);
				this.$emit('change', this.popup);
				this.$emit('input', this.popup);
			},
			check(){
				//console.log('popup',this.popup);
				let id = this.popup.id || this.popup.title || this.popup.image_url;
				if (id){
					let max_count= this.popup.max_show?this.popup.max_show:1;
					let next_delay= this.popup.next_delay?this.popup.next_delay:0;					
					console.log('max_count',max_count)
					if (next_delay>0){
						let time_key = id +'_last_time';
						let last_time = this.cache.get(time_key,0);
						let current_time = new Date().getTime()/1000;
						//console.log('last_time',last_time)
						if (last_time>0 && last_time +next_delay>current_time){
							return false;
						}
						this.cache.set(time_key,current_time);
					}
					id= id +'.popup';
					
					let show_count=this.cache.get(id,0);
					if (!show_count){
						show_count = 0;
					}
					if (show_count<max_count){
						this.cache.set(id,show_count+1);
						this.open();
					}
					
				}
			},
			submit(){
				let ret = this.validator.check(this.ruleList,this.popup.items);
				if (!ret){
					request.post(this.popup.submit_url,this.popup.items).then(res=>{
						this.dialog.msg(res.msg||'提交成功!');
						this.close();
					});
				}

				
			},
			open(val){
				//console.log('open');
				this.visible = true;
				this.openPopup('popup_dialog');
			},
			close(){
				this.visible = false;
				this.closePopup('popup_dialog');
			},
			goPopupPage(page){
				this.close();
				this.go.page((page.url?page.url:page),page.type,page.title,page.remark);
			},
		
		}
	}
</script>

